package com.orhon.smartcampus.modules.schoolaffairs.fetcher;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.schoolaffairs.dao.SchoolNewsDao;
import com.orhon.smartcampus.modules.schoolaffairs.dao.SchoolOpinionDao;
import com.orhon.smartcampus.modules.schoolaffairs.entity.SchoolNews;
import com.orhon.smartcampus.modules.schoolaffairs.entity.SchoolOpinion;
import com.orhon.smartcampus.modules.schoolaffairs.gqlModel.GQLSchoolNews;
import com.orhon.smartcampus.modules.schoolaffairs.gqlModel.GQLSchoolOpinion;
import com.orhon.smartcampus.modules.schoolaffairs.nm.SchoolNewsMapper;
import com.orhon.smartcampus.modules.schoolaffairs.nm.SchoolOpinionMapper;
import com.orhon.smartcampus.modules.student.entity.Eclass;
import com.orhon.smartcampus.modules.student.gqlModel.GQLEclass;
import com.orhon.smartcampus.modules.student.mm.EclassMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.R;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SchoolOpinionFetcher implements DataFetcher {

    @Autowired
    TokenService tokenService;

    @Autowired
    SchoolOpinionDao schoolOpinionDao;

    @Override
    public Object get(DataFetchingEnvironment env) throws Exception {
        String token = tokenService.getSystemToken();
        if(token==null) {
            return  R.NoLogin();
        }
        Long schoolId = JwtUtils.getSchoolId(token);
        int id = Integer.parseInt(env.getArgument("id").toString());
        String hql = "from SchoolOpinion where deleted_at = null and id = "+id+" and school_id = " + schoolId;
        SchoolOpinion schoolOpinion = schoolOpinionDao.findId(hql, null);
        if(schoolOpinion!=null){
//            GQLSchool gqlSchool = null;
//            if (env.getSelectionSet().contains("school")){
//                schoolOpinion.getSchoolsBySchool().toString();
//                Schools schoolsBySchool = schoolOpinion.getSchoolsBySchool();
//                gqlSchool = SchoolMapper.MAPPER.toSchool(schoolsBySchool);
//            }
            GQLSchoolOpinion gqlSchoolOpinion = SchoolOpinionMapper.MAPPER.toSchoolOpinion(schoolOpinion);
            //gqlSchoolOpinion.set(gqlSchool);
            //gqlSchoolNews.setEclass(gqlEclass);
            return gqlSchoolOpinion;
        }
        return null;
    }
}
